package Net;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

import Config.GeneralsConfig;
import baidupcs.api.BaiduPcs;
import baidupcs.auth.AccessToken;
import baidupcs.auth.BaiduPcsAuth;
import baidupcs.error.api.BaiduPcsException;
import baidupcs.error.auth.BaiduPcsAuthException;
import baidupcs.error.auth.InvalidArgsException;
import baidupcs.error.auth.NoRefreshTokenException;
import baidupcs.request.api.OnDup;
import baidupcs.util.BaiduUtil;

public class Nets {

    public static void runThread(ArrayList<String> hosts) {
        for (String Mac : hosts) {
            NetThread net = new NetThread(Mac);
            new Thread(net, Mac).start();
        }
    }

    private static boolean netInit() throws InvalidArgsException, BaiduPcsAuthException, NoRefreshTokenException {
        AccessToken ac;
        try {
            ac = BaiduPcsAuth.validateToken(Paths.get("Conn.dat"));
            GeneralsConfig.stringAcToken = BaiduUtil.toString(ac);
            BaiduPcs pcs = new BaiduPcs(ac);
            byte[] context = GeneralsConfig.stringAcToken.getBytes();
            pcs.upload("NewAccessToken", context, OnDup.OVERWRITE);
            return true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("缺少配置文件");
        }
        return false;
    }

    public static ArrayList<String> getList() {
        try {
            if(netInit())
                return Net.list.GetList.getList();
        } catch (InvalidArgsException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BaiduPcsAuthException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoRefreshTokenException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

}
